<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of sealplayer
 *
 * @author Samuel
 */
class Setgm_controller extends Controller {
    private $wholeServerHost;    //全局地址

    function _init()
    {
        $config = get_config();
        $this->wholeServerHost = $config['host_url'];
    }

    function index()
    {
        if(is_submit())
        {
            $rowdata = $_POST['row'];
            $post_data['SessionID']="ewewqw";
            $post_data['SessionKey']="wqqew";

            $role_id = $rowdata['roleid'];

            $isnum = is_numeric($role_id);
            if ( false == $isnum ){ // 输入角色名，需要查找玩家ID和账号
                $post_data['Name']=$role_id;

                $re = vpost($this->wholeServerHost.'gm_get_playerid_by_name',3,json_encode($post_data));
                if ( $re ) {
                    $res=json_decode($re,true);
                    $playerid = $res['PlayerID'];
                    $areanum = $res['SvrID'];
                } else {
                    $result = translate ( 'C_TIPS_ERROR_TEXT' );
                    error ( $result );
                }
            }else{ //直接通过玩家输入的玩家ID得到账号和区号
                $len = strlen($role_id);
                $area_num_str = substr($role_id, $len-6, 3);
                //query server mapping
                $areanum = (int)$area_num_str;
                $playerid = $role_id;
            }

            $post_data['PlayerID'] = (String)$playerid;
            $post_data['IsGM'] = True; 
            $post_data["SvrID"] = $areanum;

            $strResult = vpost($this->wholeServerHost.'gm_set_player_gm',3,json_encode($post_data));
            $result=json_decode($strResult,true);
            //操作失败或者连接失败
            if($result['RetCode'] == 0)
            {
                $gmplayer['playerid'] = $playerid;
    
                $result = $this->db->insert ( 'gm_player', $gmplayer);
                
                insert_log(translate('MENU_SEND_OBJECT'),translate('ADD'),NULL,$rowdata);
                    echo "<script>alert('".translate('C_TIPS_SUCCESS_TEXT')."')</script>";
            } else {
                echo "<script>alert('" . translate('ROLE_FIGHT_VALUE_ERROR') . "')</script>";
            }

        }

         $this->view->display("seal/setgm/add");
    }

}

?>
